package cn.iocoder.yudao.module.famous.doctor.controller.admin.dividegroups;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.dividegroups.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.dividegroups.DividegroupsDO;
import cn.iocoder.yudao.module.famous.doctor.convert.dividegroups.DividegroupsConvert;
import cn.iocoder.yudao.module.famous.doctor.service.dividegroups.DividegroupsService;

@Tag(name = "管理后台 - 我的分组")
@RestController
@RequestMapping("/famous.doctor/dividegroups")
@Validated
public class DividegroupsController {

    @Resource
    private DividegroupsService dividegroupsService;

    @PostMapping("/create")
    @Operation(summary = "创建我的分组")
    @PreAuthorize("@ss.hasPermission('famous.doctor:dividegroups:create')")
    public CommonResult<Long> createDividegroups(@Valid @RequestBody DividegroupsCreateReqVO createReqVO) {
        return success(dividegroupsService.createDividegroups(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的分组")
    @PreAuthorize("@ss.hasPermission('famous.doctor:dividegroups:update')")
    public CommonResult<Boolean> updateDividegroups(@Valid @RequestBody DividegroupsUpdateReqVO updateReqVO) {
        dividegroupsService.updateDividegroups(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:dividegroups:delete')")
    public CommonResult<Boolean> deleteDividegroups(@RequestParam("id") Long id) {
        dividegroupsService.deleteDividegroups(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:dividegroups:query')")
    public CommonResult<DividegroupsRespVO> getDividegroups(@RequestParam("id") Long id) {
        DividegroupsDO dividegroups = dividegroupsService.getDividegroups(id);
        return success(DividegroupsConvert.INSTANCE.convert(dividegroups));
    }

    @GetMapping("/list")
    @Operation(summary = "获得我的分组列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:dividegroups:query')")
    public CommonResult<List<DividegroupsRespVO>> getDividegroupsList(@RequestParam("ids") Collection<Long> ids) {
        List<DividegroupsDO> list = dividegroupsService.getDividegroupsList(ids);
        return success(DividegroupsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的分组分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:dividegroups:query')")
    public CommonResult<PageResult<DividegroupsRespVO>> getDividegroupsPage(@Valid DividegroupsPageReqVO pageVO) {
        PageResult<DividegroupsDO> pageResult = dividegroupsService.getDividegroupsPage(pageVO);
        return success(DividegroupsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出我的分组 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:dividegroups:export')")
    @OperateLog(type = EXPORT)
    public void exportDividegroupsExcel(@Valid DividegroupsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DividegroupsDO> list = dividegroupsService.getDividegroupsList(exportReqVO);
        // 导出 Excel
        List<DividegroupsExcelVO> datas = DividegroupsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "我的分组.xls", "数据", DividegroupsExcelVO.class, datas);
    }

}
